home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / c / vlclass.exe / VOLLAB.CPP < prev    next >
C/C++ Source or Header  |  1991-11-20  |  3KB  |  146 lines

  1. /* Volume Label Class
  2.    By Tom Astin 73407,3427
  3.    Copyright 1991
  4.    see VOLLAB.H for brief docs
  5. */
  6.  
  7. #include "vollab.h"
  8.  
  9. #define min(a,b)    (((a) < (b)) ? (a) : (b))
  10.  
  11. void VolLabel::SetupXFCB()
  12. {
  13.     memset(&xf,0,sizeof(xf));
  14.     xf.xfcb_flag=0xFF;
  15.     xf.xfcb_attr=8;
  16. }
  17.  
  18. void VolLabel::CustomDos(unsigned char ah, void far *data)
  19.   // class custom dos caller method
  20. {
  21.     REGS rg;
  22.     SREGS sr;
  23.  
  24.     rg.h.ah=ah;
  25.     rg.x.dx=FP_OFF(data);
  26.     sr.ds=FP_SEG(data);
  27.     res=intdosx(&rg,&rg,&sr);
  28.     bDosErr=rg.x.cflag != 0;
  29. }
  30.  
  31. unsigned short VolLabel::SectorSize(int nDrive)
  32. {
  33.     REGS rg;
  34.  
  35.     rg.h.ah=0x1C;
  36.     rg.h.dl=nDrive;
  37.     res=intdos(&rg,&rg);
  38.     bDosErr = rg.x.cflag != 0;
  39.     return (bDosErr ? 0 : rg.x.cx);
  40. }
  41.  
  42. char *VolLabel::GetLabel(int nDrive)
  43.   // get current volume label
  44. {
  45.     char far *save_dta;
  46.  
  47.     SetupXFCB();
  48.     xf.xfcb_fcb.fcb_drive=nDrive;
  49.     memset(xf.xfcb_fcb.fcb_name,'?',11);
  50.     save_dta=getdta();
  51.     setdta((char far *) &buf);
  52.     CustomDos(0x11,(void far *) &xf);     // fcb find first
  53.     setdta(save_dta);
  54.     if (!(char)res) {                      // AL=0xFF on fail
  55.         pxf=(xfcb *) &buf;
  56.         pxf->xfcb_fcb.fcb_name[11]='\0';
  57.         return pxf->xfcb_fcb.fcb_name;
  58.     }
  59.     else
  60.         return 0;
  61. }
  62.  
  63. int VolLabel::SetLabel(int nDrive, char *szLabel)
  64.   // set new label
  65. {
  66.     char *pOldv;
  67.  
  68.     if (pOldv=GetLabel(nDrive)) {  // does one exist?
  69.         memset(&fcbSpec,0,sizeof(fcbSpec));  // yes
  70.         memmove(fcbSpec.oldn,pOldv,11);
  71.         memset(fcbSpec.newn,' ',11);
  72.         memmove(fcbSpec.newn,szLabel,min(11,strlen(szLabel)));
  73.         fcbSpec.flg=0xFF;
  74.         fcbSpec.attr=8;
  75.         fcbSpec.dr=nDrive;
  76.         CustomDos(0x17,(void far *) &fcbSpec);  // fcb rename
  77.         return (!char(res));                    // AL=0xFF on fail
  78.     }
  79.     else {                     // no, doesn't exist
  80.         SetupXFCB();
  81.         xf.xfcb_fcb.fcb_drive=nDrive;
  82.         memset(&xf.xfcb_fcb.fcb_name,' ',11);    // fcb create file
  83.         memmove(&xf.xfcb_fcb.fcb_name,szLabel,min(11,strlen(szLabel)));
  84.         CustomDos(0x16,(void far *) &xf);        // AL=0xFF on fail
  85.         return (!(char)res);
  86.     }
  87. }
  88.  
  89. int VolLabel::Remove(int nDrive)
  90.   // remove current label, only Version 3 and higher recommended by Duncan
  91. {
  92.     if (_osmajor<=2) // don't delete under 2.2
  93.         return 0;
  94.  
  95.     SetupXFCB();
  96.     xf.xfcb_fcb.fcb_drive=nDrive;
  97.     memset(&xf.xfcb_fcb.fcb_name,'?',11);
  98.     CustomDos(0x13,(void far *) &xf);           // fcb delete file
  99.     return (!(char) res);                        // AL=0xFF on fail
  100. }
  101.  
  102. unsigned long VolLabel::GetSerial(unsigned char nDrive)
  103.   // return unsigned long serial number from boot record
  104. {
  105.     char *buf;
  106.     unsigned short ss=SectorSize(nDrive);
  107.     unsigned long VolID=0;
  108.  
  109.     if ((!ss | bDosErr) ||
  110.        ((buf=new char[ss+2]) == 0)) // sector size + extra for safety
  111.         return 0;
  112.     nDrive=(nDrive ? --nDrive : GetDisk());
  113.     if (absread(nDrive,1,0,buf) == 0)
  114.         VolID=*((unsigned long *) &buf[0x27]); // vol id offset
  115.     delete buf;
  116.     return VolID;
  117. }
  118.  
  119. int VolLabel::SetSerial(unsigned char nDrive, unsigned long nSerial)
  120.   // set the serial # by modifying disk boot record
  121. {
  122.     char *buf;
  123.     unsigned short ss=SectorSize(nDrive);
  124.     int bOk=0;
  125.  
  126.     if ((!ss | bDosErr) ||
  127.        ((buf=new char[ss+2]) == 0)) // sector size + extra for safety
  128.         return 0;
  129.     nDrive=(nDrive ? --nDrive : GetDisk());
  130.     if (absread(nDrive,1,0,buf) == 0) {
  131.         *((unsigned long *) &buf[0x27])=nSerial; // vol id offset
  132.         if (abswrite(nDrive,1,0,buf) == 0)
  133.             bOk=1;
  134.     }
  135.  
  136.     delete buf;
  137.     return bOk;
  138. }
  139.  
  140. unsigned char VolLabel::GetDisk()
  141. {
  142.     asm mov ah,0x19        // get current disk
  143.     asm int 0x21
  144.     return _AL;
  145. }
  146.